<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class Coin_detail extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->verify_user();
	}

	public function index()
	{
		$d['coins'] = $this->db->get('coin')->result_array();
		$coin_id = intval($this->input->get('coin_id'));

		if (!$coin_id) {
			$coin_id = $d['coins'][0]['id'];
		}

		$coin = $this->db->where('id', $coin_id)->get('coin')->row_array();
		$coin['sum'] = $this->d->sum('num+num_frozen', 'xnb', 'coin_id=' . $coin_id);
		$coin['num'] = $this->d->sum('num', 'xnb', 'coin_id=' . $coin_id);
		$coin['num_frozen'] = $this->d->sum('num_frozen', 'xnb', 'coin_id=' . $coin_id);
		$coin['fill'] = $this->d->sum('num', 'fill_coin', 'coin_id=' . $coin_id);
		$coin['draw'] = $this->d->sum('num', 'draw_coin', 'coin_id=' . $coin_id);
		$coin['deal_sum'] = $this->d->sum('num', 'deal', 'coin_id=' . $coin_id);
		$coin['deal_total'] = $this->d->sum('total', 'deal', 'coin_id=' . $coin_id);
		$coin['fill_people'] = $this->d->group_count('user_id', 'fill_coin', 'coin_id=' . $coin_id);
		$coin['draw_people'] = $this->d->group_count('user_id', 'fill_coin', 'coin_id=' . $coin_id);
		$coin['buy_deal_people'] = $this->d->group_count('buy_user_id', 'deal', 'coin_id=' . $coin_id);
		$coin['sell_deal_people'] = $this->d->group_count('sell_user_id', 'deal', 'coin_id=' . $coin_id);
		$coin['draw_fee'] = $this->d->sum('fee', 'draw_coin', 'coin_id=' . $coin_id);
		$coin['xnb_fee'] = $this->d->sum('xnb_fee', 'deal', 'coin_id=' . $coin_id);
		$coin['cny_fee'] = $this->d->sum('cny_fee', 'deal', 'coin_id=' . $coin_id);
		$coin['orders_sum'] = $this->d->sum('num-deal', 'orders', 'coin_id=' . $coin_id);
		$coin['orders_total'] = $this->d->sum('(num-deal)*price', 'orders', 'coin_id=' . $coin_id);
		$coin['buy_orders_people'] = $this->d->group_count('user_id', 'orders', 'coin_id=' . $coin_id . ' and type=0');
		$coin['sell_orders_people'] = $this->d->group_count('user_id', 'orders', 'coin_id=' . $coin_id . ' and type=1');
		$d['coin'] = $coin;
		$this->view('coin_detail', $d);
	}

	public function data()
	{
		$page = ($this->input->post('page') ? intval($this->input->post('page')) : 1);
		$rows = ($this->input->post('rows') ? intval($this->input->post('rows')) : 10);
		$id = intval($this->input->post('id'));
		$name = ($this->input->post('name') ? mysqli_real_escape_string($this->input->post('name')) : '');
		$status = intval($this->input->post('status'));

		if ($id) {
			$this->db->where('id', $id);
		}

		if ($id) {
			$this->db->where('status', $status);
		}

		if ($name) {
			$this->db->like('cn_name', $name);
			$this->db->or_like('en_name', $name);
		}

		$result['total'] = $this->db->count_all_results('coin');
		$start = ($page - 1) * $rows;
		$this->db->limit($rows, $start);
		$this->db->select();
		$rs = $this->db->get('coin');
		$result['rows'] = $rs->result_array();
		echo json_encode($result);
	}
}

?>
